package ai.equipmentManagers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import fileHandling.language.Lang;
import fileHandling.language.properties.NodeProps;
import fileHandling.language.properties.WeaponProps;

import logic.common.player.Player;
import logic.nodes.nodeSettings.upgrades.UpgradableSettings;
import logic.weapons.Weapon;
import logic.weapons.WeaponType;

public class HardEquipmentManager extends MediumEquipmentManager {
	
	protected HashMap<Weapon, Collection<WeaponProps>> weaponProps;
	protected Collection<NodeProps> hunterProps;
	
	public HardEquipmentManager(Player bot) {
		super(bot);
		
		initHunterProps();
		
		weaponProps = new HashMap<Weapon, Collection<WeaponProps>>();
		
		for(Weapon w : bot.getHunter().getWeapons(WeaponType.Energy)) {
			initWeaponProps(w);
		}
		
		for(Weapon w : bot.getHunter().getWeapons(WeaponType.HeatSeeker)) {
			initWeaponProps(w);
		}
	}
	
	@Override
	public void manageEquipment() {
		super.manageEquipment();
		
		if(hunterProps.isEmpty()) initHunterProps();
		
		upgradeProps(hunterProps, bot.getHunter().getHunterProperties());

		for(Weapon w : weaponProps.keySet()) {
			if(weaponProps.get(w).isEmpty()) initWeaponProps(w);
			upgradeProps(weaponProps.get(w), w.getWeaponProperties());
		}
	}
	
	protected void upgradeProps(Collection<? extends Lang> keys, UpgradableSettings props) {
		Iterator<? extends Lang> iter = keys.iterator();
		while(iter.hasNext()) {
			Lang key = iter.next();
			if(props.isFullyUpgraded(key.toString())) continue;
			
			int costs = props.getUpgradeCost(key.toString());
			if(costs <= bot.getMoney()) {
				upgrade(props, key);
				iter.remove();
			}
		}
	}
	
	protected void initHunterProps() {
		hunterProps = new ArrayList<NodeProps>();
		
		hunterProps.add(NodeProps.MaxHP);
		hunterProps.add(NodeProps.MaxShields);
		hunterProps.add(NodeProps.MaxEnergy);
		hunterProps.add(NodeProps.CM_Efficiency);
		hunterProps.add(NodeProps.CM_ReloadTime);
		hunterProps.add(NodeProps.MaxBoostSpeed);
		hunterProps.add(NodeProps.MaxForwardSpeed);
	}
	
	protected void initWeaponProps(Weapon weapon) {
		Collection<WeaponProps> keys = new ArrayList<WeaponProps>();
		
		keys.add(WeaponProps.Damage);
		keys.add(WeaponProps.ShootRate);
		keys.add(WeaponProps.WeaponFireSpeed);
		
		weaponProps.put(weapon, keys);
	}
}